package com.yunji.framework_template.orm.persistence.model;

import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yunji.framework_template.common.constant.Constant;
import com.yunji.framework_template.common.enumeration.NewsType;
import com.yunji.framework_template.common.util.StringUtil;
import com.yunji.framework_template.orm.persistence.service.NewsModelService;
import com.yunji.framework_template.orm.persistence.vo.ImageVO;

public class News implements Serializable {

    private static final long   serialVersionUID     = 832510338230781707L;

    private static final String FACEBOOK_COMMENT_URL = "https://graph.facebook.com/v2.4/?fields=share{comment_count}&id=";

    private Long                id;

    private String              title;

    private String              url;

    private String              urlMd5;

    private String              description;
    /**
     * 图片属性可能是多张图片，存放的图片格式可能如下：<br>
     * http://xx/1.jpg : 只有一张图片，并且没有取到图片的宽高 <br>
     * http://xx/1.jpg(800:600) : 只有一张图片，取到图片的宽高800*600 <br>
     * http://xx/1.jpg_IMG_http://xx/2.jpg : 两张图片，两张都没有取到图片的宽高 <br>
     * http://xx/1.jpg_IMG_http://xx/2.jpg(800:600) : 两张图片，第一张没有取到图片的宽高 <br>
     * http://xx/1.jpg(800:600)_IMG_http://xx/2.jpg(800:600) : 两张图片，两张都取到了图片的宽高
     */
    private String              img;

    private Date                added;

    private Integer             views;

    private Integer             comments;

    private Integer             likes;

    private Integer             dislikes;
    /**
     * 新闻的类型，用于决定将新闻插入到对应的哪个表中，他不是实际表中的字段
     */
    private String              newsType;
    /**
     * 新闻图片的list，包括
     */
    private List<ImageVO>       imgList;
    /**
     * URL的host，如http://www.baiud.com/xxx.html的host值为www.baidu.com，用于获取该HOST的ICON
     */
    private String              urlHost;

    public String getUrlHost() {
        if (!StringUtil.isEmpty(urlHost)) {
            return urlHost;
        }
        try {
            URI uri = new URI(url);
            urlHost = uri.getHost().toLowerCase();
        } catch (URISyntaxException e) {
        }
        return urlHost;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        if (title == null) {
            return Constant.EMPTY_STRING;
        }
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlMd5() {
        return urlMd5;
    }

    public void setUrlMd5(String urlMd5) {
        this.urlMd5 = urlMd5;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public Date getAdded() {
        return added;
    }

    public void setAdded(Date added) {
        this.added = added;
    }

    public Integer getViews() {
        return views;
    }

    public void setViews(Integer views) {
        this.views = views;
    }

    public Integer getComments() {
        return comments;
    }

    public void setComments(Integer comments) {
        this.comments = comments;
    }

    public Integer getLikes() {
        return likes;
    }

    public void setLikes(Integer likes) {
        this.likes = likes;
    }

    public Integer getDislikes() {
        return dislikes;
    }

    public void setDislikes(Integer dislikes) {
        this.dislikes = dislikes;
    }

    /**
     * 获取当前新闻所代表的新闻类型，如果已经有设置新闻类型，则使用设置的值，如果没有设置且ID有值，那就根据ID取值
     * 
     * @return
     */
    public String getNewsType() {
        if (newsType == null) {
            if (id > 0) {
                newsType = NewsType.getNewsType(id).name();
            }
        }
        return newsType;
    }

    public void setNewsType(String newsType) {
        this.newsType = newsType;
    }

    public List<ImageVO> getImgList() {
        if (imgList != null) {
            return imgList;
        }
        if (StringUtil.isEmpty(img)) {
            return null;
        }
        imgList = new ArrayList<ImageVO>();
        String[] imgs = img.split(Constant.IMG_SPLIT);
        for (String oneImg : imgs) {
            ImageVO imageVO = ImageVO.builder().build();
            String widthHeight = StringUtil.subStringFromLastStart(oneImg, "(", ")");
            if (!StringUtil.isEmpty(widthHeight)) {// 图片存在宽高属性
                // 获取真实的图片地址
                oneImg = oneImg.substring(0, oneImg.lastIndexOf("("));
                String[] widthHeightArr = widthHeight.split(":");
                if (widthHeightArr.length == 2) {
                    imageVO.setWidth(Integer.parseInt(widthHeightArr[0]));
                    imageVO.setHeight(Integer.parseInt(widthHeightArr[1]));
                }
            }
            imageVO.setUrl(oneImg);
            imgList.add(imageVO);
            /**
             * @return
             */
        }
        return imgList;
    }

    public long getAddedTime() {
        if (added != null) {
            return added.getTime();
        }
        return 0;
    }

    /**
     * 获取当前URL在Facebook中的评论数
     * 
     * @return
     */
    public String getFacebookCommentUrl() {
        return FACEBOOK_COMMENT_URL + NewsModelService.getSystemDomainStatic() + "/news/detail/" + id;
    }

    /**
     * 获取新闻的详情和带评论的页面
     * 
     * @return
     */
    public String getCommentUrl() {
        return NewsModelService.getSystemDomainStatic() + "/news/detail/" + id;
    }

}
